
if (typeof Effect == 'undefined')
	throw ("accordion.js requires including script.aculo.us' effects.js library!");

var accordion = Class.create();
accordion.prototype = {

		//
		// Setup the Variables
		//
		showAccordion :null,
		currentAccordion :null,
		duration :null,
		effects : [],
		animating :false,

		//  
		// Initialize the accordions
		//
		initialize : function(container, options) {
			if (!$(container)) {
				throw (container + " doesn't exist!");
				return false;
			}

			this.options = Object.extend( {
					classNames : {
							toggle :'accordion_toggle',
							toggleActive :'accordion_toggle_active',
							content :'accordion_content'
					},
					onEvent :'click',
					callback:null
			}, options || {});

			
			var accordions = $$('#' + container + ' .' + this.options.classNames.toggle);
			accordions.each( function(accordion) {
				Event.observe(accordion, this.options.onEvent, this.activate.bind(this, accordion), false);
				if (this.options.onEvent == 'click') {
					accordion.onclick = function() {
						if(this.callback !=null){
							this.callback
						}
						return false;
					}.bind(this);
				}

				this.currentAccordion = $(accordion.next(0));
			}.bind(this));
		},

		//
		// Activate an accordion
		//
		activate : function(accordion) {
			if (this.animating) {
				return false;
			}

			this.currentAccordion = $(accordion.next(0));

			this.currentAccordion.previous(0).addClassName(this.options.classNames.toggleActive);

			if (this.currentAccordion == this.showAccordion) {
				this.deactivate();
			} else {
				this._handleAccordion();
			}

		},
		// 
		// Deactivate an active accordion
		//
		deactivate : function() {

			this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);
			new Effect.SlideUp(this.showAccordion, {
				duration :0.5
			});
			this.showAccordion = null;
		},

		//
		// Handle the open/close actions of the accordion
		//
		_handleAccordion : function() {

			if (this.showAccordion) {
				this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);

				Effect.SlideUp(this.showAccordion, {
						queue : {
								scope :'myscope',
								position :'end'
						},
						duration :0.5
				});

			}
			Effect.SlideDown(this.currentAccordion, {
				queue : {
						scope :'myscope',
						position :'end'
				}
			});
			this.showAccordion = this.currentAccordion;
		}
};
